package com.mark.service.impl;

import com.mark.entity.school.*;
import com.mark.mapper.*;
import com.mark.service.UStudentService;
import com.mark.utils.StudentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * (UStudent)表服务实现类
 *
 * @author Deng PengFei
 * @since 2021-10-27 11:17:38
 */
@Service
public class UStudentServiceImpl implements UStudentService {
    @Autowired
    private UStudentMapper uStudentMapper;
    @Autowired
    private UClassMapper uClassMapper;
    @Autowired
    private UAccountMapper uAccountMapper;
    @Autowired
    private UPatriarchMapper uPatriarchMapper;
    @Autowired
    private UPatriarchStudentRelationMapper uPatriarchStudentRelationMapper;


    /**
     * 新增一个学生对象
     *
     * @param uStudent   学生对象实例
     * @param uPatriarch 学生家长实例对象
     * @param relation   亲属关系
     * @return 1001 成功 、 1002 学生已经存在，1003 失败
     */
    @Transactional
    @Override
    public Integer addStudent(UStudent uStudent, UPatriarch uPatriarch, String relation) {
        //先查询该班级是否存在该学生
        List<UStudent> students = this.uStudentMapper.queryAllByCondition(uStudent);
        if (students.size() > 0) {
            return 1002;
        }
        // 根据班级的编号查询学校的信息
        UClass uClass = this.uClassMapper.queryById(uStudent.getClaId());
        //不存在进行下面的操作 新增学生 新增家长 先向account表插入
        //获取班级的学号最后一个人的学号
        List<UStudent> students1 = this.uStudentMapper.getClaMaxStuNO(uStudent.getClaId());
        String newStuNo, year = "";
        if (students1.size() == 0) {   //如果班级没有学生 则学号为 当前年份 加 学校ID 加 年级 iD + 班级ID + 01
            SimpleDateFormat date = new SimpleDateFormat("yyyy");
            year = date.format(new Date());
            newStuNo = year + uClass.getSchId() + uClass.getGraId() + uClass.getClaId() + "01";
        } else {

            newStuNo = String.valueOf(Integer.parseInt(students1.get(0).getStuNo()) + 1);  //学号在最大的学号的基础上加1
        }
        //设置账户信息 学生向账户表插入数据
        UAccount account = new UAccount();
        account.setLoginName(newStuNo);
        account.setLoginPass(StudentUtil.getRandomPass());  // 设置生成的随机密码
        account.setRole(3);  // 设置角色为学生;
        account.setSchId(uClass.getSchId());
        account.setSchNm(uClass.getSchNm());
        this.uAccountMapper.insert(account);  // 获取插入数据的主键ID
        int accId = account.getAccId();  // 获取插入数据的主键ID
        if (accId > 0) {
            //向学生表插入数据
            uStudent.setSchId(uClass.getSchId());  //设置学校ID
            uStudent.setSchNm(uClass.getSchNm());  // 设置学校姓名
            uStudent.setClaNm(uClass.getClaNm());  //设置班级姓名
            uStudent.setStuInschYear(year);  //设置入学年份
            uStudent.setAccId(accId);      // 设置账户ID
            uStudent.setStuNo(newStuNo);  // 设学号
            this.uStudentMapper.insert(uStudent);
            //家长往账户表插入一笔数据
            account.setLoginName(newStuNo + "99");   // 家长账户
            account.setLoginPass(StudentUtil.getRandomPass());  // 设置生成的随机密码
            account.setRole(2);  // 设置角色为学生;
            account.setSchId(uClass.getSchId());
            account.setSchNm(uClass.getSchNm());
            this.uAccountMapper.insert(account);  // 获取插入数据的主键ID
            accId = account.getAccId();  // 获取插入数据的主键ID
            //向家长表插入数据
            uPatriarch.setAccId(accId);
            this.uPatriarchMapper.insert(uPatriarch);
            //家长与学生关系表
            UPatriarchStudentRelation studentRelation = new UPatriarchStudentRelation();
            studentRelation.setStuId(uStudent.getStuId());  // 学生学号
            studentRelation.setStuNm(uStudent.getStuNm()); // 学生姓名
            studentRelation.setPatId(uPatriarch.getPatId()); //家长Id
            studentRelation.setPatNm(uPatriarch.getPatNm()); // 家长姓名
            studentRelation.setRelation(relation); // 亲属关系
            int reaId = this.uPatriarchStudentRelationMapper.insert(studentRelation);
            return reaId > 0 ? 1001 : 1003;
        }
        return 1003;
    }


    /**
     * 查询指定行数据 也可以通过姓名的学号查询
     *
     * @param queryInfo 学号或者姓名
     * @param claId     班级ID
     * @return 对象列表
     */
    @Override
    public List<MyClaStuTable> getClaStuPatListByClaId(Integer claId, String queryInfo) {
        return this.uStudentMapper.getClaStuPatListByClaId(claId, queryInfo);
    }

    /**
     * 根据学号来重置学生密码
     *
     * @param stuId 学号
     * @return 存在结果
     */
    @Override
    public String restStuPassBuSno(Integer stuId) {
        //查询到学生的账户id
        UStudent student = this.uStudentMapper.queryById(stuId);
        //获取新的密码
        UAccount account = new UAccount();
        account.setAccId(student.getAccId());
        account.setLoginPass(StudentUtil.getRandomPass());
        account = this.uAccountMapper.queryById(student.getAccId());
        return account.getLoginPass();
    }


    /**
     * 批量导入学生信息
     *
     * @param stuList 读取到的学生excl表格
     * @param claId   班级id
     */
    @Override
    public void importStuList(ArrayList<ArrayList<String>> stuList, Integer claId) {
        //解析学生信息
        ArrayList<String> title = stuList.get(0);   // 获取到标题行
        List<UStudent> students = new ArrayList<>();
        List<UPatriarch> patriarchs = new ArrayList<>();
        List<String> relationList = new ArrayList<>();
        // 获取内容行
        for (ArrayList<String> stuInfo : stuList) {
            UStudent student = new UStudent();
            UPatriarch patriarch = new UPatriarch();
            String relation = "";
            for (int j = 0; j < stuInfo.size(); j++) {
                switch (title.get(j).trim()) {
                    case "姓名":
                        student.setStuNm(stuInfo.get(j).trim());
                        break;
                    case "性别":
                        student.setStuSex(Integer.parseInt(stuInfo.get(j).trim()));
                        break;
                    case "家长姓名":
                        patriarch.setPatNm(stuInfo.get(j).trim());
                        break;
                    case "家长电话":
                        patriarch.setPatMobile(stuInfo.get(j).trim());
                        break;
                    case "亲属关系":
                        relation = stuInfo.get(j).trim();
                        break;
                    default:
                        break;
                }
            }
            student.setClaId(claId);
            students.add(student);
            patriarchs.add(patriarch);
            relationList.add(relation);
        }
        for (int i = 0; i < students.size(); i++) {
            this.addStudent(students.get(i), patriarchs.get(i), relationList.get(i));
        }

    }


    /**
     * 批量删除学生信息
     *
     * @param members 学生成员
     * @return true 成功 false 失败
     */
    @Transactional
    @Override
    public Boolean delStuList(List<MyClaStuTable> members) {
        // 需要删除3张表的数据
        for (MyClaStuTable member : members) {
            this.uStudentMapper.deleteById(member.getStuId());
            this.uPatriarchMapper.deleteById(member.getPatId());
            this.uPatriarchStudentRelationMapper.deleteByStuId(member.getStuId());
        }
        return true;
    }


    /**
     * 学生转移班级
     *
     * @param members 要转移的成员
     * @param newCla  新的班级
     * @return true 成功 ，false 失败
     */
    @Override
    public Boolean shiftStuList(List<MyClaStuTable> members, Integer newCla) {
        boolean flag = false;
        // 通过班级Id查询到班级信息
        UClass uClass = this.uClassMapper.queryById(newCla);
        //修改学生信息表格
        for (MyClaStuTable member : members) {
            UStudent student = new UStudent();
            student.setStuId(member.getStuId());
            student.setClaId(uClass.getClaId());
            student.setClaNm(uClass.getClaNm());
            this.uStudentMapper.update(student);
            flag = true;
        }
        return flag;
    }
}